<?php

class Adverts
{
	 
	 public static function getAdvertsCount()
	 {
		 return SQL::num_rows(Template::render("getAdverts.sql", "sql", array ('pu_users_id'=>$_SESSION['user_id'])));	  
	 }
	 
	  public static function getAdvertById($p_id)
	 {
		$data= SQL::select(Template::render("getAdvertById.sql", "sql", array ('id'=>$p_id)));
		 
		 if ($data)
		 {
			 return $data;
		 }
		 else
		 {
			 return false;
		 }			
	 }
	
	
	public static function  getAdverts($p_start_page)
	{
		 $query=Template::render("getAdverts.sql", "sql", array ('pu_users_id'=>$_SESSION['user_id']));
		 
		 if ($p_start_page==1)
		 {
			 $query= $query.' LIMIT 10';
		 }
		 else
		 {
			 $page=($p_start_page-1)*10;
			 $query=$query." LIMIT $page, 10";
		 }
		 
		 $data=SQL::select($query);
		 
		 if ($data)
		 {
			 return $data;
		 }
		 else
		 {
			 return false;
		 }		 
	}
	
	 public static function getQuery()
	 {
		 return Template::render("getAdverts.sql", "sql", array ('pu_users_id'=>$_SESSION['user_id']));	  
	 }	
	
	public static function createAdvert($p_rubric, $p_category, $p_ekip_type, $p_text, $p_phone, $p_email, $p_skype)
	{
		 SQL::execute(Template::render("createAdvert.sql", "sql", array
		 (
			 'rubric'=>$p_rubric,
			 'category'=>$p_category,
			 'ekip_type'=>$p_ekip_type,
			 'text'=>$p_text,
			 'date'=>time(),
			 'phone'=>$p_phone,
			 'email'=>$p_email,
			 'skype'=>$p_skype,
			 'pu_users_id'=>$_SESSION['user_id']
		 )));		 
	}
	
	public static function  editAdvert($p_id, $p_rubric, $p_category, $p_ekip_type, $p_text, $p_phone, $p_email, $p_skype)
	{
		 SQL::execute(Template::render("editAdvert.sql", "sql", array
		 (
			 'id'=>$p_id,
			 'rubric'=>$p_rubric,
			 'category'=>$p_category,
			 'ekip_type'=>$p_ekip_type,
			 'text'=>$p_text,
			 'date'=>time(),
			 'phone'=>$p_phone,
			 'email'=>$p_email,
			 'skype'=>$p_skype,
			 'user_id'=>$_SESSION['user_id']
		 )));
	}
	
	public static function  deleteAdvert($p_id)
	{
		  SQL::execute(Template::render("deleteAdvert.sql", "sql", array
		 (
			 'id'=>$p_id,			
			 'pu_users_id'=>$_SESSION['user_id']
		 )));
	}

}